/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * <p>Project: jse2302 - Ex1
 * <p>Powered by webrx On 2023-06-30 09:26:37
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex1 {
    public static void main(String[] args) {
        String.format("%tF %<tT 启动关机程序的",System.currentTimeMillis());
        //var t = new Thread(new Shutdown(1));
        //t.start();

        Calendar c = Calendar.getInstance();
        c.set(2023, 7 - 1, 1, 12, 20, 30);

        var t = new Thread(new Shut(args[0]));
        t.start();

    }
}


class Shut implements Runnable {
    private long now = System.currentTimeMillis();


    public Shut(String str){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            this.now = sdf.parse(str).getTime();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public Shut(Date date) {

        this.now = date.getTime();
    }

    public Shut(LocalDateTime localDateTime) {

        this.now = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public Shut(Calendar calendar) {
        this.now = calendar.getTimeInMillis();
    }

    public Shut(long now) {
        this.now = now;
    }

    public Shut() {
    }

    public long getNow() {
        return now;
    }

    public void setNow(long now) {
        this.now = now;
    }

    @Override
    public void run() {
        long t = now - System.currentTimeMillis();
        try {
            //System.out.printf("系统在：%tF %<tT关机....%n", this.now);
            Thread.sleep(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("关机.....");
        System.out.println();
    }


}


class Shutdown implements Runnable {
    private int time = 10;

    public Shutdown() {
    }

    public Shutdown(int time) {
        this.time = time;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    @Override
    public void run() {
        //System.out.printf("系统%d分钟后关机....", this.time);
        try {
            TimeUnit.MINUTES.sleep(this.time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("关机.....");
    }
}
